<template>
  <div class="fdsCleaningRuleset-add-update-component">
    <el-dialog
      v-if="modelValue.visible"
      v-model="modelValue.visible"
      :title="
        !modelValue.form.id
          ? `新增${modelValue.title}`
          : `修改${modelValue.title}`
      "
      width="550px"
    >
      <div style="padding-right: 50px; padding-top: 20px">
        <el-form
          :model="modelValue.form"
          :rules="modelValue.rules"
          ref="addEditRef"
          :label-width="100"
        >
          <el-form-item label="规则编号" prop="number">
            <el-input
              placeholder="请输入规则编号"
              v-model="modelValue.form.number"
              autocomplete="off"
            />
          </el-form-item>
          <el-form-item label="规则名称" prop="name">
            <el-input
              placeholder="请输入规则名称"
              v-model="modelValue.form.name"
              autocomplete="off"
            />
          </el-form-item>
          <el-form-item label="是否启用" prop="enabled">
            <el-input
              placeholder="请输入是否启用"
              v-model="modelValue.form.enabled"
              autocomplete="off"
            />
          </el-form-item>
          <el-form-item label="启用前状态" prop="enabledBefore">
            <el-input
              placeholder="请输入启用前状态"
              v-model="modelValue.form.enabledBefore"
              autocomplete="off"
            />
          </el-form-item>
          <el-form-item label="生效日期" prop="dayTime">
            <el-input
              placeholder="请输入生效日期"
              v-model="modelValue.form.dayTime"
              autocomplete="off"
            />
          </el-form-item>
          <el-form-item label="默认值" prop="defaultValue">
            <el-input
              placeholder="请输入默认值"
              v-model="modelValue.form.defaultValue"
              autocomplete="off"
            />
          </el-form-item>
          <el-form-item label="备注信息" prop="remark">
            <el-input
              placeholder="请输入备注信息"
              v-model="modelValue.form.remark"
              autocomplete="off"
            />
          </el-form-item>
          <el-form-item label="休息类型" prop="restType">
            <el-input
              placeholder="请输入休息类型"
              v-model="modelValue.form.restType"
              autocomplete="off"
            />
          </el-form-item>
          <el-form-item label="是否需要提醒" prop="needRemind">
            <el-input
              placeholder="请输入是否需要提醒"
              v-model="modelValue.form.needRemind"
              autocomplete="off"
            />
          </el-form-item>
          <el-form-item label="步骤数量" prop="stepCount">
            <el-input
              placeholder="请输入步骤数量"
              v-model="modelValue.form.stepCount"
              autocomplete="off"
            />
          </el-form-item>
          <el-form-item label="步骤数量" prop="stepCountInt">
            <el-input
              placeholder="请输入步骤数量"
              v-model="modelValue.form.stepCountInt"
              autocomplete="off"
            />
          </el-form-item>
          <el-form-item label="是否需要批量清洁" prop="needBatchClean">
            <el-input
              placeholder="请输入是否需要批量清洁"
              v-model="modelValue.form.needBatchClean"
              autocomplete="off"
            />
          </el-form-item>
          <el-form-item label="是否需要清洁空闲设备" prop="needCleanEmpty">
            <el-input
              placeholder="请输入是否需要清洁空闲设备"
              v-model="modelValue.form.needCleanEmpty"
              autocomplete="off"
            />
          </el-form-item>
          <el-form-item label="是否丢弃前置物料" prop="beforeDiscardMaterial">
            <el-input
              placeholder="请输入是否丢弃前置物料"
              v-model="modelValue.form.beforeDiscardMaterial"
              autocomplete="off"
            />
          </el-form-item>
          <el-form-item label="是否为基础设置" prop="baseline">
            <el-input
              placeholder="请输入是否为基础设置"
              v-model="modelValue.form.baseline"
              autocomplete="off"
            />
          </el-form-item>
          <el-form-item label="清洁模式" prop="cleanMode">
            <el-input
              placeholder="请输入清洁模式"
              v-model="modelValue.form.cleanMode"
              autocomplete="off"
            />
          </el-form-item>
          <el-form-item label="子设置" prop="children">
            <el-input
              placeholder="请输入子设置"
              v-model="modelValue.form.children"
              autocomplete="off"
            />
          </el-form-item>
        </el-form>
      </div>
      <template #footer v-if="!modelValue.form.disabled">
        <span class="dialog-footer">
          <el-button @click="cancel">取消</el-button>
          <el-button type="primary" @click="ensure"> 确定 </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { ElMessage, FormInstance } from 'element-plus'
import {
  addFdsCleaningRuleset,
  updateFdsCleaningRuleset,
} from '@/api/fds-cleaning-ruleset.api'

const props = defineProps(['modelValue'])
const addEditRef = ref<FormInstance>()

/***
 * 确认弹框
 */
const ensure = () => {
  let form = props.modelValue.form
  let formData = {
    number: form.number,
    name: form.name,
    enabled: form.enabled,
    enabledBefore: form.enabledBefore,
    dayTime: form.dayTime,
    defaultValue: form.defaultValue,
    remark: form.remark,
    restType: form.restType,
    needRemind: form.needRemind,
    stepCount: form.stepCount,
    stepCountInt: form.stepCountInt,
    needBatchClean: form.needBatchClean,
    needCleanEmpty: form.needCleanEmpty,
    beforeDiscardMaterial: form.beforeDiscardMaterial,
    baseline: form.baseline,
    cleanMode: form.cleanMode,
    children: form.children,
  }

  if (!addEditRef.value) return
  addEditRef.value.validate((valid, fields) => {
    if (valid) {
      if (form.id) {
        updateFdsCleaningRuleset({
          ...formData,
          id: form.id,
        })
          .then((res) => {
            cancel()
            ElMessage({
              message: props.modelValue.title + '修改成功',
              type: 'success',
            })
            if (props.modelValue.refreshDataList) {
              props.modelValue.refreshDataList()
            }
          })
          .catch((err) => {
            ElMessage({
              message: err.msg || err,
              type: 'error',
            })
          })
        return
      }

      addFdsCleaningRuleset(formData)
        .then((res) => {
          cancel()
          ElMessage({
            message: props.modelValue.title + '新增成功',
            type: 'success',
          })
          if (props.modelValue.refreshDataList) {
            props.modelValue.refreshDataList()
          }
        })
        .catch((err) => {
          ElMessage({
            message: err.msg || err,
            type: 'error',
          })
        })
    }
  })
}

/***
 * 取消弹框
 */
const cancel = () => {
  props.modelValue.visible = false
}
</script>
<style lang="scss" scoped></style>
